package com.crm.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.Business;
import com.crm.entity.User;
import com.crm.util.ApplicationContext;



public class UserDAO {
	protected final Log logger = LogFactory.getLog(getClass());

	public  int insert(User u)throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     session.saveOrUpdate(u);
		     tx.commit();
		     return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
		
	}
	public  User getUserDet(String userName) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     Query query=session.createQuery("from User WHERE UNAME=:userName");
				query.setString("userName", userName);
				Object ob=query.uniqueResult();
				User u=(User)ob;
		     tx.commit();
		     return u;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}

	public  User validatCredentials(String user, String pass) throws Exception {
		
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Query query = session.createQuery("from User where (email_id=:email or uname=:user) and pwd=:pass");
			query.setString("email", user);
			query.setString("user", user);
			query.setString("pass", pass);
			User u = (User) query.uniqueResult();
			return u;
		}catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
		
	}


	public User getUserLoginDetails(String email) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			 Query query = session.createQuery("from User where email_id=:email");
			 query.setString("email", email);
			 User u = (User) query.uniqueResult();
			 return u;
		}catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public User getUserDetails(int busid,int userid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		System.out.println("Session is conected "+ session.isConnected()+" is open "+session.isOpen());
		
		try {
			Query query = session.createQuery("from User where business_id=:busid and id=:id");
			query.setInteger("busid", busid);
			query.setInteger("id", userid);
			User u = (User) query.uniqueResult();
			return u;
		}catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public List<User> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			Query query = session.createQuery("from User where business_id=:busid and is_deleted!=true");
			query.setInteger("busid", busid);
			List<User> userList = (List<User>) query.list();
			return userList;
		}catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public void deleteUser(int userId, int businessId) throws HibernateException, Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
		     Query query = session.createQuery("update User set is_deleted=true where business_id=:businessId and id=:userId");
		     query.setInteger("userId", userId);
		     query.setInteger("businessId", businessId);
		     query.executeUpdate();
		     tx.commit();
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}

	

}
